<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

$staffId = $_GET['staff_id'];

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['start_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$startYear = $getDateArray[0] * 1;
$startMonth = $getDateArray[1] * 1;
$startDay = $getDateArray[2] * 1;

$startYear = $startYear > 0 ? $startYear : $tmp_year;
$startMonth = $startMonth > 0 ? $startMonth : $tmp_mon;
$startDay = $startDay > 0 ? $startDay : $tmp_day;


$searchDate = $_GET['end_date'];
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$endYear = $getDateArray[0] * 1;
$endMonth = $getDateArray[1] * 1;
$endDay = $getDateArray[2] * 1;

$endYear = $endYear > 0 ? $endYear : $tmp_year;
$endMonth = $endMonth > 0 ? $endMonth : $tmp_mon;
$endDay = $endDay > 0 ? $endDay : $tmp_day;


$startTime = mktime(0, 0, 0, $startMonth, $startDay, $startYear);
$endTime = mktime(23,59,59, $endMonth, $endDay, $endYear);

//Logs::setLog("startTime = ".date("y-m-d H:i", $startTime)."||endTime=".date("y-m-d H:i", $endTime));
$temp = WorkSchedule::getWorkTime($staffId, $startTime, $endTime);
$workScheduleTime = $temp[0]['sum_work'];
$temp = Absence::getRestTimeSum($staffId, $startTime, $endTime);
$sumRest = $temp[0]['sum_rest'];
//Logs::setLog(" $workScheduleTime  =============> $sumRest ");

$workTime = $workScheduleTime - $sumRest;

$result = array();

$result['work_schedule_hour'] = floor($workScheduleTime / 3600);
$result['work_schedule_minute'] = ($workScheduleTime % 3600)/60;
$result['rest_hour'] = floor($sumRest / 3600);
$result['rest_minute'] = ($sumRest % 3600)/60;
$result['work_hour'] = floor($workTime / 3600);
$result['work_minute'] = ($workTime % 3600)/60;

$temp = Staff::getById($staffId);
$result['salary'] = $temp['salary'];
$result['salary_type'] = $temp['salary_type'];


if( $result == null ){
	Output::Json("user is null", -1);
}else{
	Output::Json($result);
}
